package com.hps.demo.config;

import com.hps.demo.serialization.ProtostuffSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 *
 * @author heps
 * @since 2020/10/29
 */
//@Configuration
public class ProtostuffRedisConfig {

  /**
   * redisTemplate 序列化使用的 Serializable, 存储二进制字节码, 所以自定义序列化类
   * @param redisConnectionFactory {@link RedisConnectionFactory}
   * @return redisTemplate
   */
  @Bean
  public RedisTemplate<Object, Object> protoStuffTemplate(
      RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<Object, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);

    // redis value使用的序列化器
    template.setValueSerializer(new ProtostuffSerializer());
    // redis key使用的序列化器
    template.setKeySerializer(new StringRedisSerializer());

    template.afterPropertiesSet();
    return template;
  }
}
